package com.cxx.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxx.home.dto.bill.BillCategoryDto;
import com.cxx.home.dto.bill.BillItemDto;
import com.cxx.framework.web.CustomException;
import com.cxx.home.entity.BillBook;
import com.cxx.home.entity.BillCategory;
import com.cxx.home.entity.BillPay;
import com.cxx.home.mapper.BillBookMapper;
import com.cxx.home.mapper.BillCategoryMapper;
import com.cxx.home.mapper.BillPayMapper;
import com.cxx.home.service.BillManageService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class BillManageServiceImpl implements BillManageService {
    @Resource
    private BillPayMapper billPayMapper;

    @Resource
    private BillBookMapper billBookMapper;

    @Resource
    private BillCategoryMapper billCategoryMapper;

    @Override
    public Boolean addBillPay(BillItemDto billItemDto) {
        // 1. 校验是否存在
        LambdaQueryWrapper<BillPay> queryWrapper = new LambdaQueryWrapper<>();
        if (billPayMapper.exists(queryWrapper.eq(BillPay::getName, billItemDto.getName()))) {
            throw new CustomException("该账户已经存在");
        }

        // 2. 新增账户
        BillPay newbillPay = new BillPay();
        BeanUtils.copyProperties(billItemDto, newbillPay);

        return billPayMapper.insert(newbillPay) == 1;
    }

    @Override
    public Boolean updateBillPay(Long id, BillItemDto billItemDto) {
        // 1. 校验是否存在
        if (Objects.isNull(billPayMapper.selectById(id))) {
            throw new RuntimeException("该账户不存在");
        }

        // 2. 更新账户
        BillPay billPay = new BillPay();
        BeanUtils.copyProperties(billItemDto, billPay);
        billPay.setId(id);

        return billPayMapper.updateById(billPay) == 1;
    }

    @Override
    public Boolean addBillBook(BillItemDto billItemDto) {
        // 1. 校验是否存在
        LambdaQueryWrapper<BillBook> queryWrapper = new LambdaQueryWrapper<>();
        if (billBookMapper.exists(queryWrapper.eq(BillBook::getName, billItemDto.getName()))) {
            throw new CustomException("该账本已经存在");
        }

        // 2. 新增账本
        BillBook billBook = new BillBook();
        BeanUtils.copyProperties(billItemDto, billBook);

        return billBookMapper.insert(billBook) == 1;
    }

    @Override
    public Boolean updateBillBook(Long id, BillItemDto billItemDto) {
        // 1. 校验是否存在
        if (Objects.isNull(billBookMapper.selectById(id))) {
            throw new CustomException("该账单不存在");
        }

        // 2. 更新账本
        BillBook billBook = new BillBook();
        BeanUtils.copyProperties(billItemDto, billBook);
        billBook.setId(id);

        return billBookMapper.updateById(billBook) == 1;
    }

    private Long getBillBook(String name) {
        LambdaQueryWrapper<BillBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BillBook::getName, name);
        BillBook billBook = billBookMapper.selectOne(queryWrapper);

        if (Objects.isNull(billBook)) {
            throw new CustomException("该账本类型不存在");
        }

        return billBook.getId();
    }

    @Override
    public Boolean addBillCategory(BillCategoryDto billCategoryDto) {
        // 1. 校验是否存在
        LambdaQueryWrapper<BillCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (billCategoryMapper.exists(queryWrapper.eq(BillCategory::getName, billCategoryDto.getName()))) {
            throw new CustomException("该账单类型已经存在");
        }

        // 2. 新增账单类型
        BillCategory newBillCategory = new BillCategory();
        BeanUtils.copyProperties(billCategoryDto, newBillCategory);
        newBillCategory.setBookId(getBillBook(billCategoryDto.getBookName()));

        return billCategoryMapper.insert(newBillCategory) == 1;
    }

    @Override
    public Boolean updateBillCategory(Long id, BillCategoryDto billCategoryDto) {
        // 1. 校验是否存在
        if (Objects.isNull(billCategoryMapper.selectById(id))) {
            throw new CustomException("该账单类型不存在");
        }

        // 2. 更新账单类型
        BillCategory billCategory = new BillCategory();
        BeanUtils.copyProperties(billCategoryDto, billCategory);
        billCategory.setBookId(getBillBook(billCategoryDto.getBookName()));
        billCategory.setId(id);

        return billCategoryMapper.updateById(billCategory) == 1;
    }
}
